package com.jd.ty.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.jd.ty.service.EmployeeService;
import com.jd.ty.service.FunctionService;
import com.jd.ty.service.RoleService;
import com.jd.ty.service.UserService;
import com.jd.ty.service.exception.ErrorMessage;
import com.jd.ty.service.exception.ServiceException;
import com.jd.ty.util.DES;
import com.jd.ty.util.Pager;
import com.jd.ty.web.constant.SessionKey;
import com.jd.ty.web.filter.EmployeeFilter;
import com.jd.ty.web.filter.UserFilter;
import com.jd.ty.pojo.Employee;
import com.jd.ty.pojo.Function;
import com.jd.ty.pojo.Permission;
import com.jd.ty.pojo.Role;
import com.jd.ty.pojo.User;

public class RoleAction extends BaseAction {

	private static final long serialVersionUID = -4018571604337249963L;

	private Long id;

	private RoleService roleService;

	private FunctionService functionService;
	private Role role = new Role();

	private List<Function> functions;
	private Long[] permission;

	public RoleService getRoleService() {
		return roleService;
	}

	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public Role getRole() {
		return role;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	@Override
	public String execute() throws Exception {

		return SUCCESS;
	}

	public String list() {
		pager = getPagerObject();
		List<Role> res = (List<Role>) roleService.getList(pager, null, getSortStr());
		pager.setDataRows(res);
		return SUCCESS;

	}

	public String detail() {
		if (id != null) {
			role = roleService.get(id);
		}
		functions = functionService.getTree();

		return SUCCESS;
	}

	public String update() throws ServiceException {
		try {
			if(!role.isAdmin()){
				role.setPermissionIds(permission);
			}
			if (role.getRoleId() == null) {
				roleService.add(role);
			} else {
				roleService.update(role);
			}
		} catch (ServiceException e) {
			addActionError(e.getErrorMessage());
			return INPUT;
		}
		return SUCCESS;
	}

	public String delete() {
		try {
			roleService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			return ERROR;
		}
		return SUCCESS;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public Pager getPager() {
		return pager;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {

		this.page = page;

	}

	public FunctionService getFunctionService() {
		return functionService;
	}

	public void setFunctionService(FunctionService functionService) {
		this.functionService = functionService;
	}

	public List<Function> getFunctions() {
		return functions;
	}

	public void setFunctions(List<Function> functions) {
		this.functions = functions;
	}

	public Long[] getPermission() {
		return permission;
	}

	public void setPermission(Long[] permission) {
		this.permission = permission;
	}

}
